import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:hao_ke/pages/order/order_item_widget.dart';
import 'package:hao_ke/pages/utils/OrderModel.dart';
import 'package:hao_ke/utils/shared_preferences.dart';
import 'package:hao_ke/widgets/search_bar_widget.dart';
import 'package:scoped_model/scoped_model.dart';

import '../../consts/url_const.dart';
import '../../utils/dio_http.dart';

class OrderIndex extends StatefulWidget{




  @override
  State createState() {
    return _TabIndexState();
  }

  const OrderIndex({super.key});
}

class _TabIndexState extends State<OrderIndex>{
  List<Widget> list = [];
  int currentIndex = 1;
  final ScrollController _controller = ScrollController(
    initialScrollOffset: 0.0,
    keepScrollOffset: true,
  );

  @override
  Widget build(BuildContext context) {
    return  ScopedModel<OrderModel>(
        model: OrderModel(),
        child: Scaffold(
          appBar: AppBar(
            title: SearchBarWidget(locationFunc: () {},),
            backgroundColor: Colors.white,
            centerTitle: true,
          ),
          body: RefreshIndicator(
            onRefresh: _refreshData,
            child: Container(
                decoration: BoxDecoration(
                    gradient: LinearGradient(
                        begin: Alignment.topCenter,
                        end: Alignment.bottomCenter,
                        colors: [
                          Color.fromRGBO(200, 100, 100, 0.5),
                          Color.fromRGBO(0, 0, 200, 0.5),
                        ]
                    )
                ),
                child: ListView(
                  controller: _controller,
                  children: list,
                )
            )
          ),
        )
    );
  }


  Future<void> _refreshData() async {
    currentIndex = 1;
    return await _getListItem(context);
  }

  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        _getListItem(context);
      }
    });
    _getListItem(context);
  }

  Future<void> _getListItem(context) async {
    if(currentIndex==1) {
      setState(() {
        list = [];
      });
    }
    var token = await SharedPreferencesUtils.getInstance().getString('token');
    var response = await DioHttp()
        .init(context, token)
        .get(UrlConst.order_list, params: { 'current': currentIndex, 'size': 20});
    var res = response['pages']['records'];
    List<Widget> tempList = [];
    for (var item in res) {
      tempList.add(OrderItemWidget(
        item['projectName'],
        item['orderNo'],
        item['amount'].toString(),
        item['userName']==null?'':item['godNo'].toString(),
        item['phone']==null?'':item['phone'].toString(),
        item['state'].toString(),
        serviceDate:
        item['serviceDate'] == null ? '' : item['serviceDate'].toString(),
        employeeName: item['serviceEmployeeName'] == null
            ? ''
            : item['serviceEmployeeName'].toString(),
      ));
    }

    if(currentIndex==1) {
      setState(() {
        list = tempList;
      });
    }else{
      setState(() {
        list.addAll(tempList);
      });
    }

    if(tempList.length<20) {
      setState(() {
        list.add(
          Container(
            height: 50,
            alignment: Alignment.center,
            child: const Text('没有更多数据了'),
          )
        );
      });
    }

    currentIndex = currentIndex + 1;
  }

}